<template>
  <div class="app-container">
    <el-card style="margin: 15px; min-height: calc(100vh - 111px);">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="楼宇名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入楼宇名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼宇地址" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入楼宇所在地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="buildingList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="楼宇名称" align="center" prop="name" />
      <el-table-column label="楼宇地址" align="center" prop="location" />
      <el-table-column label="楼宇总层数" align="center" prop="floor" />
      <el-table-column prop="createTime" label="创建时间" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 添加或修改楼宇信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼宇名称"  prop="name">
          <el-input v-model="form.name" placeholder="请输入楼宇名称" />
        </el-form-item>
        <el-form-item label="楼宇所在位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入楼宇所在位置" />
        </el-form-item>
        <el-form-item label="楼宇总层数" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼宇总层数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  listBuilding,
  getBuilding,
  delBuilding,
  addBuilding,
  updateBuilding,
} from "@/api/building";

export default {
  name: "BuildingView",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼宇信息表格数据
      buildingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        pageSize: 10,
        name: null,
        location: null,
        floor: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "楼宇名称不能为空", trigger: "blur" },
        ],
        location: [
          { required: true, message: "楼宇所在位置不能为空", trigger: "blur" },
        ],
        floor: [
          { required: true, message: "楼宇总层数不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 重置表单验证状态
    resetFormValidation() {
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    /** 查询楼宇信息列表 */
    getList() {
      this.loading = true;
      listBuilding(this.queryParams).then((response) => {
        if(response.data.code === 200){
          this.buildingList = response.data.data.rows;
          this.total = response.data.data.total;
          this.loading = false;
        }else{
          this.$message({
                type: "warning",
                message: response.data.msg,
              });
        }
        
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        location: null,
        floor: null,
        createTime: null,
        updateTime: null,
      };
      this.resetFormValidation(); // 重置表单验证状态
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼宇信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBuilding(id).then((response) => {
        if(response.data.code === 200){
          this.form = response.data.data;
          this.open = true;
          this.title = "修改楼宇信息";
        }else{
          this.$message({
                type: "warning",
                message: response.data.msg,
              });
        }
        
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBuilding(this.form).then((resp) => {
              if (resp.data.code !== 200) {
                this.$message({
                  message: resp.data.msg,
                  type: "warning",
                });
                this.open = false;
                this.getList();
                return;
              }
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.open = false;
              this.getList();
            });
          } else {
            addBuilding(this.form).then((resp) => {
              if (resp.data.code !== 200) {
                this.$message({
                  message: resp.data.msg,
                  type: "warning",
                });
                this.open = false;
                this.getList();
                return;
              }
              this.$message({
                message: "添加成功",
                type: "success",
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除楼宇', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => 
      {
        delBuilding(ids).then((resp) => {
          if (resp.data.code !== 200) {
            this.$message({
              message: resp.data.msg,
              type: "warning",
            });
            return;
          }
          this.$message({
          type: 'success',
          message: '删除成功!'
        });
          this.getList();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.page = val;
      this.getList();
    },
  },
};
</script>
